﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace MenusHunter.Services
{
    /// <summary>
    /// Summary description for PhotosManager1
    /// </summary>
    [WebService(Namespace = "http://menuhunter.com/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class PhotosManager1 : System.Web.Services.WebService
    {
        
        [WebMethod]
        public List<Entities.Photo> GetListPhotoRestaurant(int resturantId)
        {
            using (var db = new MenusHunterDataContext())
            {
                return (from i in db.Photos
                        where i.RestaurantId == resturantId && i.Status == 1
                        select new Entities.Photo()
                                   {
                                       Id = i.Id,
                                       RestaurantId = (int) i.RestaurantId,
                                       Url = i.Url,
                                       UrlThumb = i.UrlThumb,
                                       DateCreate = (DateTime) i.DateCreate,
                                       Status = (int) i.Status
                                   }).ToList();
            }
        }
        
        
        [WebMethod]
        public int NewPhoto(int restaurantId, string url, string urlThumb, int status)
        {
            using (var db = new MenusHunterDataContext())
            {
                var item = new Photo()
                               {
                                   RestaurantId = restaurantId,
                                   Url = url,
                                   UrlThumb = urlThumb,
                                   DateCreate = DateTime.Now,
                                   Status = status
                               };

                db.Photos.InsertOnSubmit(item);
                db.SubmitChanges();

                return item.Id;
            }
        }

        [WebMethod]
        public bool DeletePhoto(int photoId)
        {
            using (var db = new MenusHunterDataContext())
            {
                try
                {
                    var item = (from i in db.Photos where i.Id == photoId select i).SingleOrDefault();

                    if (item != null)
                    {
                        db.Photos.DeleteOnSubmit(item);

                        db.SubmitChanges();
                        return true;
                    }
                    return false;
                }catch(Exception)
                {
                    return false;
                }
            }
        }
    }
}
